<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreCurrency {
	var $currencies;
	var $default_currency;
	var $current_currency;
	
	//constructor
	function CoreCurrency() {
		$this->currencies = array();
		if( defined('CONFIG_STORE_DEFAULT_CURRENCY') ) {
			$this->default_currency = CONFIG_STORE_DEFAULT_CURRENCY;
		} else {
			$this->default_currency = 'USD';
		}
		$this->current_currency = $this->default_currency;
		
		$this->currencies = $this->_get_currencies_default();
	}
	
	function getCurrentCurrency() {
		return $this->current_currency;
	}
	
	//methods
	function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
		if( empty($currency_type)) $currency_type = $this->current_currency;
		
		if( $calculate_currency_value == true) {
			$rate = ($this->_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
			$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format($this->_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
			
			// if the selected currency is in the european euro-conversion and the default currency is euro,
			// the currency will displayed in the national currency and euro currency
			if( ($this->default_currency == 'EUR') && ($currency_type == 'DEM' || $currency_type == 'BEF' || $currency_type == 'LUF' || $currency_type == 'ESP' || $currency_type == 'FRF' || $currency_type == 'IEP' || $currency_type == 'ITL' || $currency_type == 'NLG' || $currency_type == 'ATS' || $currency_type == 'PTE' || $currency_type == 'FIM' || $currency_type == 'GRD') ) {
				$format_string .= ' <small>[' . $this->format($number, true, 'EUR') . ']</small>';
			}
		} else {
			$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format($this->_round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
		}
		
		return $format_string;
	}
	
	function is_set($code) {
		if( isset($this->currencies[$code]) && $this->_not_null($this->currencies[$code]) ) {
			return true;
		} else {
			return false;
		}
	}
	
	function get_value($code) {
		return $this->currencies[$code]['value'];
	}
	
	function get_decimal_places($code) {
		return $this->currencies[$code]['decimal_places'];
	}
	
	function display_price($products_price, $products_tax_rate, $quantity = 1) {
		return $this->format(TaxUtil::add_tax($products_price, $products_tax_rate) * $quantity);
	}
	
	// Checks to see if the currency code exists as a currency
	// TABLES: currencies
	function checkCurrencyExists($code) {
		return $this->_check_currency_exists($code);
	}
	
	//private
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _round($number, $precision) {
		return util_round($number, $precision);
	}
	
	function _get_currencies($is_active = true) {
		global $adb;
		
		//Fields
		//	currencies_id, currencies_code, currencies_name, 
		//	currencies_symbol_left, currencies_symbol_right, 
		//	currencies_decimal_point, currencies_thousands_point, 
		//	currencies_decimal_places, currencies_value		
		
		$currencies = array();
		if( $is_active == true ) {
			$query = "select * from pp_core_currencies where currencies_status = 1";
		} else {
			$query = "select * from pp_core_currencies";
		}
		$result = $adb->query($query);
		while($row = $adb->fetch_array($result)) {
			$currencies[$row['currencies_code']] = array(
										'name' => $row['currencies_name'],
										'symbol_left' => $row['currencies_symbol_left'],
										'symbol_right' => $row['currencies_symbol_right'],
										'decimal_point' => $row['currencies_decimal_point'],
										'thousands_point' => $row['currencies_thousands_point'],
										'decimal_places' => $row['currencies_decimal_places'],
										'value' => $row['currencies_value']
												);
		}
		
		return $currencies;
	}
	
	function _get_currencies_default() {
		$currencies = array();
		$currencies['USD'] = array(
						'name' => 'US Dollar',
						'symbol_left' => '$',
						'symbol_right' => '',
						'decimal_point' => '.',
						'thousands_point' => '3',
						'decimal_places' => '2',
						'value' => 1,
							);
		
		return $currencies;
	}
	
	function _check_currency_exists($code) {
		global $adb;
		
		$code = func_db_input($code);
		
		$query = "select currencies_id from pp_core_currencies where currencies_code = '" . $code . "'";
		$result = $adb->query($query);
		$num_rows = $adb->num_rows($result);
		if( $num_rows > 0 ) {
			return true;
		} else {
			return false;
		}
	}
}

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class CoreCurrencyBase extends BaseItemBase {
	
	//constructor
	function CoreCurrencyBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_core_currencies';
		$this->prefix = 'currencies_';
		$this->_table_owner_usable = false;
	}
	
	//methods
	function getDefault() {
		return 'usd';
	}
}

class CurrencyUtil {
	
	//constructor
	function CoreCurrencyUtil() {
		
	}
	
	function init() {
		
	}
	
	//methods
	function getDefault() {
		$ccyObj = new CoreCurrencyBase();
		
		return $ccyObj->getDefault();
	}
	
	//update currency exchange rates
	//Possible values are 'oanda' 'xe' or ''
	function updateQuote($code, $base = '', $type = 'oanda') {
		
	}
	
	function updateQuotes($base = '', $type = 'oanda') {
		
	}
	
	function _update_quote_oanda($code, $base) {
		$page = file('http://www.oanda.com/convert/fxdaily?value=1&redirected=1&exch=' . $code . '&format=CSV&dest=Get+Table&sel_list=' . $base);
		$matches = array();
		preg_match('/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i', implode('', $page), $matches);
		if( sizeof($matches) > 0 ) {
			return $matches['3'];
		}
		
		return false;
	}
	
	function _update_quote_xe($to, $from) {
		$page = file('http://www.xe.net/ucc/convert.cgi?Amount=1&From=' . $from . '&To=' . $to);
		$matches = array();
		preg_match('/[0-9.]+\s*' . $from . '\s*=\s*([0-9.]+)\s*' . $to . '/', implode('', $page), $matches);
		if( sizeof($matches) > 0 ) {
			return $matches['1'];
		}
		
		return false;
	}
}
//
?>